package com.aaa.controller;

import com.aaa.entity.Message;
import com.aaa.entity.Pager;
import com.aaa.entity.SysRoleEntity;
import com.aaa.service.RoleService;
import com.aaa.service.RoleServiceImpl;
import com.aaa.service.UserService;
import com.aaa.service.UserServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "RoleServlet",urlPatterns = "/role.action")
public class RoleServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if (method!=null)
        {
            switch (method)
            {
                case "add":
                    add(request,response);
                    break;
                case "query":
                    query(request,response);
                    break;
                    //为角色绑定菜单
                case "queryMenus":
                    queryMenus(request,response);
                    break;
                    //设置权限
                case "setMenus":
                    setMenus(request,response);
                    break;
                case "deleterole":
                    deleterole(request,response);
                    break;
                case "updateone":
                    updateone(request,response);
                    break;
            }

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String role_id=request.getParameter("role_id1");
        String role_name=request.getParameter("role_name1");  //
        String role_key=request.getParameter("role_key1");
        String status=request.getParameter("status1");
        String remark=request.getParameter("remark1");
//        String startTime=request.getParameter("startTime1");
//        String endTime=request.getParameter("endTime1");

        RoleService roleservice=new RoleServiceImpl();
        SysRoleEntity info=new SysRoleEntity((long)Integer.valueOf(role_id),role_name,role_key,status,remark);
        int res= roleservice.create(info);
        PrintWriter writer = response.getWriter();
        writer.print(JSON.toJSONString(res));

    }
    protected void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageNo = request.getParameter("pageNo")==null?"1":request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize")==null?"1":request.getParameter("pageNo");
        //查询条件
        Map<String,String> condition=new HashMap<>();
        condition.put("role_name", request.getParameter("role_name"));  //
        condition.put("role_key", request.getParameter("role_key"));
        condition.put("status", request.getParameter("status"));
        condition.put("startTime", request.getParameter("startTime"));
        condition.put("endTime", request.getParameter("endTime"));

        RoleService service=new RoleServiceImpl();
        Pager pager=service.query(Integer.valueOf(pageNo), Integer.valueOf(pageSize), condition);
        response.getWriter().print(JSON.toJSONString(pager));
    }
    protected void queryMenus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RoleService service=new RoleServiceImpl();
        int roleid=Integer.parseInt(request.getParameter("roleid"));
        List list= service.queryMenusByRoleId(roleid);
        //将java对象转换成json的字符串
        String str=JSON.toJSONString(list);
        response.getWriter().print(str);
    }
    protected void setMenus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RoleService service=new RoleServiceImpl();
        int roleid=Integer.parseInt(request.getParameter("roleid"));
        String ids=request.getParameter("ids");
        //转换成数组
        String[] menuIds = ids.split(",");

        Message message = service.setMenus(roleid, menuIds);

        //将java对象转换成json的字符串
        String str=JSON.toJSONString(message);
        response.getWriter().print(str);
    }
    protected void deleterole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String role_id=request.getParameter("role_id");
        RoleService service=new RoleServiceImpl();
        int res=service.delete(Integer.parseInt(role_id));
        String str=JSON.toJSONString(res);
        response.getWriter().print(str);
    }
    private void updateone(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        String role_id=request.getParameter("role_id");
        RoleService service=new RoleServiceImpl();
        Map map=service.queryid(Integer.valueOf(role_id));
        String str= JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
        response.getWriter().print(str);
    }
}
